﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Logging Application Block
//===============================================================================

using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity;
using Data = Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
using Microsoft.Practices.ObjectBuilder2;
using EntLibContrib.Logging.TraceListeners;

namespace EntLibContrib.Logging.Database.Configuration.Unity
{
	/// <summary>
	/// This type supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
	/// Represents the process to create the container policies required to create a <see cref="ExtendedDatabaseTraceListener"/>.
	/// </summary>
	public class ExtendedDatabaseTraceListenerPolicyCreator : IContainerPolicyCreator
	{
		#region Public Methods
		void IContainerPolicyCreator.CreatePolicies(
			IPolicyList policyList,
			string instanceName,
			ConfigurationElement configurationObject,
			IConfigurationSource configurationSource)
		{
			new PolicyBuilder<ExtendedDatabaseTraceListener, ExtendedDatabaseTraceListenerData>(
				instanceName,
				(ExtendedDatabaseTraceListenerData)configurationObject,
				c => new ExtendedDatabaseTraceListener(
					Resolve.Reference<Data.Database>(c.DatabaseInstanceName),
					c.WriteLogSqlCommand,
					c.AddCategorySqlCommand,
					c.CommandType,
					Resolve.OptionalReference<ILogFormatter>(c.Formatter)))
				.AddPoliciesToPolicyList(policyList);
		}
		#endregion
	}
}
